{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Options Arbitrage"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Import Dependencies\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "from black_scholes import call_value, put_value, call_delta, put_delta, call_vega, put_vega\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "def read_data(filename):\n",
    "    df = pd.read_csv(filename, index_col=0)\n",
    "\n",
    "    time_to_expiry = df.filter(like='TimeToExpiry')\n",
    "\n",
    "    stock = df.filter(like='Stock')\n",
    "    stock.columns = [stock.columns.str[-5:], stock.columns.str[:-6]]\n",
    "\n",
    "    options = pd.concat((df.filter(like='-P'), df.filter(like='-C')), axis=1)\n",
    "    options.columns = [options.columns.str[-3:], options.columns.str[:-4]]\n",
    "\n",
    "    market_data = pd.concat((stock, options), axis=1)\n",
    "\n",
    "    return time_to_expiry, market_data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "# Read the market data\n",
    "filename = 'Options Arbitrage.csv'\n",
    "time_to_expiry, market_data = read_data(filename)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Stock', 'P60', 'P70', 'P80', 'C60', 'C70', 'C80']\n",
      "['P60', 'P70', 'P80', 'C60', 'C70', 'C80']\n"
     ]
    }
   ],
   "source": [
    "# Get a list of all instrument names including the stock, and of the options only\n",
    "instrument_names = list(market_data.columns.get_level_values(0).unique())\n",
    "print(instrument_names)\n",
    "\n",
    "option_names = instrument_names[1:]\n",
    "print(option_names)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Add time_to_expiry to market_data\n",
    "market_data['TTE'] = time_to_expiry['TimeToExpiry']\n",
    "\n",
    "# Store timestamp in variable to prevent\n",
    "# errors with multiplications and such\n",
    "timestamp = market_data.index\n",
    "\n",
    "# Set the Time to Expiry as Index\n",
    "market_data = market_data.set_index('TTE')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "# Create Empty Dictionaries\n",
    "short_call_values = {}\n",
    "long_call_values = {}\n",
    "long_put_values = {}\n",
    "short_put_values = {}\n",
    "short_call_deltas = {}\n",
    "long_call_deltas = {}\n",
    "long_put_deltas = {}\n",
    "short_put_deltas = {}\n",
    "option_values = {}\n",
    "option_deltas = {}\n",
    "\n",
    "# Set known attributes\n",
    "r = 0\n",
    "sigma = 0.20\n",
    "\n",
    "# Forloop to create new columns with Call/Put names\n",
    "for option in option_names:\n",
    "    # Retrieve K from the Option\n",
    "    K = int(option[-2:])\n",
    "\n",
    "    if 'C' in option:\n",
    "        short_call_values[option] = []\n",
    "        long_call_values[option] = []\n",
    "        short_call_deltas[option] = []\n",
    "        long_call_deltas[option] = []\n",
    "\n",
    "        # Forloop to calculate short/long call values and deltas\n",
    "        for time, stock_value in market_data.iterrows():\n",
    "            short_call_values[option].append(call_value(\n",
    "                stock_value['Stock', 'AskPrice'], K, time, r, sigma))\n",
    "            long_call_values[option].append(call_value(\n",
    "                stock_value['Stock', 'BidPrice'], K, time, r, sigma))\n",
    "            long_call_deltas[option].append(call_delta(\n",
    "                stock_value['Stock', 'BidPrice'], K, time, r, sigma))\n",
    "            short_call_deltas[option].append(-call_delta(\n",
    "                stock_value['Stock', 'AskPrice'], K, time, r, sigma))\n",
    "\n",
    "        option_values['Short Call', option] = short_call_values[option]\n",
    "        option_values['Long Call', option] = long_call_values[option]\n",
    "        option_deltas['Short Call', option] = short_call_deltas[option]\n",
    "        option_deltas['Long Call', option] = long_call_deltas[option]\n",
    "\n",
    "    if 'P' in option:\n",
    "        long_put_values[option] = []\n",
    "        short_put_values[option] = []\n",
    "        long_put_deltas[option] = []\n",
    "        short_put_deltas[option] = []\n",
    "\n",
    "        # Forloop to calculate short/long put values and deltas\n",
    "        for time, stock_value in market_data.iterrows():\n",
    "            long_put_values[option].append(\n",
    "                put_value(stock_value['Stock', 'AskPrice'], K, time, r, sigma))\n",
    "            short_put_values[option].append(\n",
    "                put_value(stock_value['Stock', 'BidPrice'], K, time, r, sigma))\n",
    "            long_put_deltas[option].append(\n",
    "                put_delta(stock_value['Stock', 'AskPrice'], K, time, r, sigma))\n",
    "            short_put_deltas[option].append(-put_delta(\n",
    "                stock_value['Stock', 'BidPrice'], K, time, r, sigma))\n",
    "\n",
    "        option_values['Long Put', option] = long_put_values[option]\n",
    "        option_values['Short Put', option] = short_put_values[option]\n",
    "        option_deltas['Long Put', option] = long_put_deltas[option]\n",
    "        option_deltas['Short Put', option] = short_put_deltas[option]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Option Values\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr:last-of-type th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th colspan=\"3\" halign=\"left\">Long Call</th>\n",
       "      <th colspan=\"3\" halign=\"left\">Long Put</th>\n",
       "      <th colspan=\"3\" halign=\"left\">Short Call</th>\n",
       "      <th colspan=\"3\" halign=\"left\">Short Put</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th>C60</th>\n",
       "      <th>C70</th>\n",
       "      <th>C80</th>\n",
       "      <th>P60</th>\n",
       "      <th>P70</th>\n",
       "      <th>P80</th>\n",
       "      <th>C60</th>\n",
       "      <th>C70</th>\n",
       "      <th>C80</th>\n",
       "      <th>P60</th>\n",
       "      <th>P70</th>\n",
       "      <th>P80</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>TTE</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0.911625</th>\n",
       "      <td>12.04</td>\n",
       "      <td>5.71</td>\n",
       "      <td>2.24</td>\n",
       "      <td>1.31</td>\n",
       "      <td>4.92</td>\n",
       "      <td>11.39</td>\n",
       "      <td>12.21</td>\n",
       "      <td>5.82</td>\n",
       "      <td>2.29</td>\n",
       "      <td>1.34</td>\n",
       "      <td>5.01</td>\n",
       "      <td>11.54</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0.911615</th>\n",
       "      <td>12.08</td>\n",
       "      <td>5.74</td>\n",
       "      <td>2.25</td>\n",
       "      <td>1.29</td>\n",
       "      <td>4.88</td>\n",
       "      <td>11.32</td>\n",
       "      <td>12.29</td>\n",
       "      <td>5.88</td>\n",
       "      <td>2.32</td>\n",
       "      <td>1.33</td>\n",
       "      <td>4.99</td>\n",
       "      <td>11.50</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0.911606</th>\n",
       "      <td>12.17</td>\n",
       "      <td>5.79</td>\n",
       "      <td>2.28</td>\n",
       "      <td>1.28</td>\n",
       "      <td>4.86</td>\n",
       "      <td>11.29</td>\n",
       "      <td>12.33</td>\n",
       "      <td>5.91</td>\n",
       "      <td>2.34</td>\n",
       "      <td>1.32</td>\n",
       "      <td>4.94</td>\n",
       "      <td>11.43</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0.911596</th>\n",
       "      <td>12.12</td>\n",
       "      <td>5.76</td>\n",
       "      <td>2.27</td>\n",
       "      <td>1.28</td>\n",
       "      <td>4.86</td>\n",
       "      <td>11.29</td>\n",
       "      <td>12.33</td>\n",
       "      <td>5.91</td>\n",
       "      <td>2.34</td>\n",
       "      <td>1.32</td>\n",
       "      <td>4.96</td>\n",
       "      <td>11.47</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0.911587</th>\n",
       "      <td>12.08</td>\n",
       "      <td>5.74</td>\n",
       "      <td>2.25</td>\n",
       "      <td>1.29</td>\n",
       "      <td>4.88</td>\n",
       "      <td>11.32</td>\n",
       "      <td>12.29</td>\n",
       "      <td>5.88</td>\n",
       "      <td>2.32</td>\n",
       "      <td>1.33</td>\n",
       "      <td>4.99</td>\n",
       "      <td>11.50</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         Long Call             Long Put              Short Call              \\\n",
       "               C60   C70   C80      P60   P70    P80        C60   C70   C80   \n",
       "TTE                                                                           \n",
       "0.911625     12.04  5.71  2.24     1.31  4.92  11.39      12.21  5.82  2.29   \n",
       "0.911615     12.08  5.74  2.25     1.29  4.88  11.32      12.29  5.88  2.32   \n",
       "0.911606     12.17  5.79  2.28     1.28  4.86  11.29      12.33  5.91  2.34   \n",
       "0.911596     12.12  5.76  2.27     1.28  4.86  11.29      12.33  5.91  2.34   \n",
       "0.911587     12.08  5.74  2.25     1.29  4.88  11.32      12.29  5.88  2.32   \n",
       "\n",
       "         Short Put               \n",
       "               P60   P70    P80  \n",
       "TTE                              \n",
       "0.911625      1.34  5.01  11.54  \n",
       "0.911615      1.33  4.99  11.50  \n",
       "0.911606      1.32  4.94  11.43  \n",
       "0.911596      1.32  4.96  11.47  \n",
       "0.911587      1.33  4.99  11.50  "
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Option Deltas\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr:last-of-type th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th colspan=\"3\" halign=\"left\">Long Call</th>\n",
       "      <th colspan=\"3\" halign=\"left\">Long Put</th>\n",
       "      <th colspan=\"3\" halign=\"left\">Short Call</th>\n",
       "      <th colspan=\"3\" halign=\"left\">Short Put</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th>C60</th>\n",
       "      <th>C70</th>\n",
       "      <th>C80</th>\n",
       "      <th>P60</th>\n",
       "      <th>P70</th>\n",
       "      <th>P80</th>\n",
       "      <th>C60</th>\n",
       "      <th>C70</th>\n",
       "      <th>C80</th>\n",
       "      <th>P60</th>\n",
       "      <th>P70</th>\n",
       "      <th>P80</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>TTE</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0.911625</th>\n",
       "      <td>0.830169</td>\n",
       "      <td>0.558665</td>\n",
       "      <td>0.290582</td>\n",
       "      <td>-0.166116</td>\n",
       "      <td>-0.435503</td>\n",
       "      <td>-0.704329</td>\n",
       "      <td>-0.833884</td>\n",
       "      <td>-0.564497</td>\n",
       "      <td>-0.295671</td>\n",
       "      <td>0.169831</td>\n",
       "      <td>0.441335</td>\n",
       "      <td>0.709418</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0.911615</th>\n",
       "      <td>0.831105</td>\n",
       "      <td>0.560126</td>\n",
       "      <td>0.291851</td>\n",
       "      <td>-0.164281</td>\n",
       "      <td>-0.432599</td>\n",
       "      <td>-0.701776</td>\n",
       "      <td>-0.835719</td>\n",
       "      <td>-0.567401</td>\n",
       "      <td>-0.298224</td>\n",
       "      <td>0.168895</td>\n",
       "      <td>0.439874</td>\n",
       "      <td>0.708149</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0.911606</th>\n",
       "      <td>0.832963</td>\n",
       "      <td>0.563041</td>\n",
       "      <td>0.294394</td>\n",
       "      <td>-0.163369</td>\n",
       "      <td>-0.431150</td>\n",
       "      <td>-0.700497</td>\n",
       "      <td>-0.836631</td>\n",
       "      <td>-0.568850</td>\n",
       "      <td>-0.299503</td>\n",
       "      <td>0.167037</td>\n",
       "      <td>0.436959</td>\n",
       "      <td>0.705606</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0.911596</th>\n",
       "      <td>0.832038</td>\n",
       "      <td>0.561584</td>\n",
       "      <td>0.293120</td>\n",
       "      <td>-0.163368</td>\n",
       "      <td>-0.431150</td>\n",
       "      <td>-0.700499</td>\n",
       "      <td>-0.836632</td>\n",
       "      <td>-0.568850</td>\n",
       "      <td>-0.299501</td>\n",
       "      <td>0.167962</td>\n",
       "      <td>0.438416</td>\n",
       "      <td>0.706880</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0.911587</th>\n",
       "      <td>0.831108</td>\n",
       "      <td>0.560126</td>\n",
       "      <td>0.291847</td>\n",
       "      <td>-0.164278</td>\n",
       "      <td>-0.432599</td>\n",
       "      <td>-0.701779</td>\n",
       "      <td>-0.835722</td>\n",
       "      <td>-0.567401</td>\n",
       "      <td>-0.298221</td>\n",
       "      <td>0.168892</td>\n",
       "      <td>0.439874</td>\n",
       "      <td>0.708153</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         Long Call                      Long Put                      \\\n",
       "               C60       C70       C80       P60       P70       P80   \n",
       "TTE                                                                    \n",
       "0.911625  0.830169  0.558665  0.290582 -0.166116 -0.435503 -0.704329   \n",
       "0.911615  0.831105  0.560126  0.291851 -0.164281 -0.432599 -0.701776   \n",
       "0.911606  0.832963  0.563041  0.294394 -0.163369 -0.431150 -0.700497   \n",
       "0.911596  0.832038  0.561584  0.293120 -0.163368 -0.431150 -0.700499   \n",
       "0.911587  0.831108  0.560126  0.291847 -0.164278 -0.432599 -0.701779   \n",
       "\n",
       "         Short Call                     Short Put                      \n",
       "                C60       C70       C80       P60       P70       P80  \n",
       "TTE                                                                    \n",
       "0.911625  -0.833884 -0.564497 -0.295671  0.169831  0.441335  0.709418  \n",
       "0.911615  -0.835719 -0.567401 -0.298224  0.168895  0.439874  0.708149  \n",
       "0.911606  -0.836631 -0.568850 -0.299503  0.167037  0.436959  0.705606  \n",
       "0.911596  -0.836632 -0.568850 -0.299501  0.167962  0.438416  0.706880  \n",
       "0.911587  -0.835722 -0.567401 -0.298221  0.168892  0.439874  0.708153  "
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Create DataFrames with index market_data\n",
    "option_values = pd.DataFrame(option_values, index=market_data.index)\n",
    "option_deltas = pd.DataFrame(option_deltas, index=market_data.index)\n",
    "\n",
    "# Sort the DataFrames\n",
    "option_values = option_values.reindex(sorted(option_values.columns), axis=1)\n",
    "option_deltas = option_deltas.reindex(sorted(option_deltas.columns), axis=1)\n",
    "\n",
    "# Rounding\n",
    "option_values = round(option_values, 2)\n",
    "\n",
    "# Show DataFrames\n",
    "print('Option Values')\n",
    "display(option_values.head())\n",
    "print('Option Deltas')\n",
    "display(option_deltas.head())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Create Columns for Black Scholes Value in the Data Set\n",
    "# This is used for later calculations (algorithm and such)\n",
    "\n",
    "for option in option_names:\n",
    "    if \"C\" in option:\n",
    "        market_data[option,\n",
    "                    'Expected AskPrice'] = option_values['Short Call', option]\n",
    "        market_data[option,\n",
    "                    'Expected BidPrice'] = option_values['Long Call', option]\n",
    "        market_data[option,\n",
    "                    'Delta Short'] = option_deltas['Short Call', option].values\n",
    "        market_data[option,\n",
    "                    'Delta Long'] = option_deltas['Long Call', option].values\n",
    "\n",
    "    elif \"P\" in option:\n",
    "        market_data[option,\n",
    "                    'Expected AskPrice'] = option_values['Short Put', option]\n",
    "        market_data[option,\n",
    "                    'Expected BidPrice'] = option_values['Long Put', option]\n",
    "        market_data[option,\n",
    "                    'Delta Short'] = option_deltas['Short Put', option].values\n",
    "        market_data[option,\n",
    "                    'Delta Long'] = option_deltas['Long Put', option].values\n",
    "\n",
    "# Sort Columns\n",
    "market_data = market_data.reindex(sorted(market_data.columns), axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "BidPrice is at least 0.10 higher than Expected AskPrice for Option C80\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>AskPrice</th>\n",
       "      <th>AskVolume</th>\n",
       "      <th>BidPrice</th>\n",
       "      <th>BidVolume</th>\n",
       "      <th>Delta Long</th>\n",
       "      <th>Delta Short</th>\n",
       "      <th>Expected AskPrice</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>TTE</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0.910616</th>\n",
       "      <td>2.47</td>\n",
       "      <td>22.0</td>\n",
       "      <td>2.40</td>\n",
       "      <td>161.0</td>\n",
       "      <td>0.287906</td>\n",
       "      <td>-0.294257</td>\n",
       "      <td>2.28</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0.908790</th>\n",
       "      <td>2.49</td>\n",
       "      <td>19.0</td>\n",
       "      <td>2.41</td>\n",
       "      <td>19.0</td>\n",
       "      <td>0.292730</td>\n",
       "      <td>-0.296557</td>\n",
       "      <td>2.30</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0.863975</th>\n",
       "      <td>2.34</td>\n",
       "      <td>21.0</td>\n",
       "      <td>2.27</td>\n",
       "      <td>15.0</td>\n",
       "      <td>0.282434</td>\n",
       "      <td>-0.288892</td>\n",
       "      <td>2.17</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0.846851</th>\n",
       "      <td>2.29</td>\n",
       "      <td>14.0</td>\n",
       "      <td>2.22</td>\n",
       "      <td>18.0</td>\n",
       "      <td>0.281149</td>\n",
       "      <td>-0.285046</td>\n",
       "      <td>2.11</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          AskPrice  AskVolume  BidPrice  BidVolume  Delta Long  Delta Short  \\\n",
       "TTE                                                                           \n",
       "0.910616      2.47       22.0      2.40      161.0    0.287906    -0.294257   \n",
       "0.908790      2.49       19.0      2.41       19.0    0.292730    -0.296557   \n",
       "0.863975      2.34       21.0      2.27       15.0    0.282434    -0.288892   \n",
       "0.846851      2.29       14.0      2.22       18.0    0.281149    -0.285046   \n",
       "\n",
       "          Expected AskPrice  \n",
       "TTE                          \n",
       "0.910616               2.28  \n",
       "0.908790               2.30  \n",
       "0.863975               2.17  \n",
       "0.846851               2.11  "
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "AskPrice is at least 0.10 lower than Expected BidPrice for Option C80\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>AskPrice</th>\n",
       "      <th>AskVolume</th>\n",
       "      <th>BidPrice</th>\n",
       "      <th>BidVolume</th>\n",
       "      <th>Delta Long</th>\n",
       "      <th>Delta Short</th>\n",
       "      <th>Expected BidPrice</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>TTE</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0.906355</th>\n",
       "      <td>2.03</td>\n",
       "      <td>17.0</td>\n",
       "      <td>1.96</td>\n",
       "      <td>19.0</td>\n",
       "      <td>0.282249</td>\n",
       "      <td>-0.288574</td>\n",
       "      <td>2.14</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0.890925</th>\n",
       "      <td>1.70</td>\n",
       "      <td>122.0</td>\n",
       "      <td>1.63</td>\n",
       "      <td>21.0</td>\n",
       "      <td>0.258776</td>\n",
       "      <td>-0.262483</td>\n",
       "      <td>1.87</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0.869368</th>\n",
       "      <td>1.31</td>\n",
       "      <td>86.0</td>\n",
       "      <td>1.25</td>\n",
       "      <td>19.0</td>\n",
       "      <td>0.218205</td>\n",
       "      <td>-0.222875</td>\n",
       "      <td>1.45</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0.847146</th>\n",
       "      <td>1.96</td>\n",
       "      <td>143.0</td>\n",
       "      <td>1.88</td>\n",
       "      <td>19.0</td>\n",
       "      <td>0.279899</td>\n",
       "      <td>-0.285090</td>\n",
       "      <td>2.06</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0.831088</th>\n",
       "      <td>2.43</td>\n",
       "      <td>14.0</td>\n",
       "      <td>2.35</td>\n",
       "      <td>16.0</td>\n",
       "      <td>0.324083</td>\n",
       "      <td>-0.329540</td>\n",
       "      <td>2.54</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0.755689</th>\n",
       "      <td>3.13</td>\n",
       "      <td>22.0</td>\n",
       "      <td>3.05</td>\n",
       "      <td>21.0</td>\n",
       "      <td>0.388099</td>\n",
       "      <td>-0.395452</td>\n",
       "      <td>3.23</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0.754014</th>\n",
       "      <td>3.09</td>\n",
       "      <td>95.0</td>\n",
       "      <td>3.00</td>\n",
       "      <td>18.0</td>\n",
       "      <td>0.386434</td>\n",
       "      <td>-0.392318</td>\n",
       "      <td>3.21</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          AskPrice  AskVolume  BidPrice  BidVolume  Delta Long  Delta Short  \\\n",
       "TTE                                                                           \n",
       "0.906355      2.03       17.0      1.96       19.0    0.282249    -0.288574   \n",
       "0.890925      1.70      122.0      1.63       21.0    0.258776    -0.262483   \n",
       "0.869368      1.31       86.0      1.25       19.0    0.218205    -0.222875   \n",
       "0.847146      1.96      143.0      1.88       19.0    0.279899    -0.285090   \n",
       "0.831088      2.43       14.0      2.35       16.0    0.324083    -0.329540   \n",
       "0.755689      3.13       22.0      3.05       21.0    0.388099    -0.395452   \n",
       "0.754014      3.09       95.0      3.00       18.0    0.386434    -0.392318   \n",
       "\n",
       "          Expected BidPrice  \n",
       "TTE                          \n",
       "0.906355               2.14  \n",
       "0.890925               1.87  \n",
       "0.869368               1.45  \n",
       "0.847146               2.06  \n",
       "0.831088               2.54  \n",
       "0.755689               3.23  \n",
       "0.754014               3.21  "
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The amount of trades are 11\n"
     ]
    }
   ],
   "source": [
    "def option_opportunities(option):\n",
    "    '''\n",
    "    This function gives arbitrage opportunities based on whether the price\n",
    "    of the option is too high or too low. The results are used to 'eyeball' \n",
    "    if our final results match what this function displays. This works for \n",
    "    all Calls and Puts.\n",
    "    '''\n",
    "    if \"C\" in option:\n",
    "        expected1 = market_data[option][(market_data[option, 'BidPrice'] - market_data[option,\n",
    "                                                                                       'Expected AskPrice']) >= 0.10].drop('Expected BidPrice', axis=1)\n",
    "        expected2 = market_data[option][(market_data[option, 'Expected BidPrice'] -\n",
    "                                         market_data[option, 'AskPrice']) >= 0.10].drop('Expected AskPrice', axis=1)\n",
    "\n",
    "    elif \"P\" in option:\n",
    "        expected1 = market_data[option][(market_data[option, 'BidPrice'] - market_data[option,\n",
    "                                                                                       'Expected AskPrice']) >= 0.10].drop('Expected BidPrice', axis=1)\n",
    "        expected2 = market_data[option][(market_data[option, 'Expected BidPrice'] -\n",
    "                                         market_data[option, 'AskPrice']) >= 0.10].drop('Expected AskPrice', axis=1)\n",
    "\n",
    "    print('BidPrice is at least 0.10 higher than Expected AskPrice for Option ' + option)\n",
    "    display(expected1)\n",
    "    print('AskPrice is at least 0.10 lower than Expected BidPrice for Option ' + option)\n",
    "    display(expected2)\n",
    "    print('The amount of trades are', len(expected1) + len(expected2))\n",
    "\n",
    "option_opportunities('C80')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Create a Dictionary with Timestamp and Time to Expiry\n",
    "# Index of market_data was changed earlier to time to expiry\n",
    "trades = {('Timestamp', ''): timestamp,\n",
    "          ('Time to Expiry', ''): market_data.index}\n",
    "\n",
    "# Forloop that adds columns for the Call/Put Positions and Deltas\n",
    "# Global function is a changing variable name based on the option\n",
    "# For option C60 it will create a variable named positions_call_C60\n",
    "for option in option_names:\n",
    "\n",
    "    if 'C' in option:\n",
    "        trades['Call Position', option] = []\n",
    "        trades['Call Delta', option] = []\n",
    "        globals()['positions_call_' + option] = 0\n",
    "\n",
    "    if 'P' in option:\n",
    "        trades['Put Position', option] = []\n",
    "        trades['Put Delta', option] = []\n",
    "        globals()['positions_put_' + option] = 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Forloop over the rows of market_data\n",
    "for time, data in market_data.iterrows():\n",
    "\n",
    "    max_delta = min(data['Stock', 'AskVolume'], data['Stock', 'BidVolume'])\n",
    "\n",
    "    # Forloop over the option_names with conditions\n",
    "    # if-statements if Call or Put + if Short/Long in Call or Put\n",
    "    for option in option_names:\n",
    "\n",
    "        if 'C' in option:\n",
    "\n",
    "            # Short Call\n",
    "            if (data[option, 'BidPrice'] - data[option, 'Expected AskPrice']) >= 0.10:\n",
    "                short_call_volume = data[option, 'BidVolume']\n",
    "                long_call_volume = 0\n",
    "\n",
    "            # Long Call\n",
    "            elif (data[option, 'Expected BidPrice'] - data[option, 'AskPrice']) >= 0.10:\n",
    "                long_call_volume = data[option, 'AskVolume']\n",
    "                short_call_volume = 0\n",
    "\n",
    "            else:\n",
    "                long_call_volume = short_call_volume = 0\n",
    "\n",
    "            call_trade = long_call_volume - short_call_volume\n",
    "\n",
    "            # Define variable, as set earlier. Note the first position is set to zero otherwise\n",
    "            # One would get an error here since the variable is then not yet defined.\n",
    "            globals()['positions_call_' + option] = call_trade + \\\n",
    "                globals()['positions_call_' + option]\n",
    "\n",
    "            # Add Positions (cumulative)\n",
    "            trades['Call Position', option].append(\n",
    "                globals()['positions_call_' + option])\n",
    "\n",
    "            if globals()['positions_call_' + option] >= 0:\n",
    "                long_call_delta = data[option, 'Delta Long']\n",
    "                short_call_delta = 0\n",
    "\n",
    "            elif globals()['positions_call_' + option] < 0:\n",
    "                short_call_delta = data[option, 'Delta Short']\n",
    "                long_call_delta = 0\n",
    "\n",
    "            # Add Deltas (cumulative)\n",
    "            trades['Call Delta', option].append(\n",
    "                abs(globals()['positions_call_' + option]) * (long_call_delta + short_call_delta))\n",
    "\n",
    "        if 'P' in option:\n",
    "\n",
    "            # Short Put\n",
    "            if (data[option, 'BidPrice'] - data[option, 'Expected AskPrice']) >= 0.10:\n",
    "                short_put_volume = data[option, 'BidVolume']\n",
    "                long_put_volume = 0\n",
    "\n",
    "            # Long Put\n",
    "            elif (data[option, 'Expected BidPrice'] - data[option, 'AskPrice']) >= 0.10:\n",
    "                long_put_volume = data[option, 'AskVolume']\n",
    "                short_put_volume = 0\n",
    "\n",
    "            else:\n",
    "                long_put_volume = short_put_volume = 0\n",
    "\n",
    "            put_trade = long_put_volume - short_put_volume\n",
    "\n",
    "            globals()['positions_put_' + option] = put_trade + \\\n",
    "                globals()['positions_put_' + option]\n",
    "\n",
    "            trades['Put Position', option].append(\n",
    "                globals()['positions_put_' + option])\n",
    "\n",
    "            if globals()['positions_put_' + option] >= 0:\n",
    "                long_put_delta = data[option, 'Delta Long']\n",
    "                short_put_delta = 0\n",
    "\n",
    "            elif globals()['positions_put_' + option] < 0:\n",
    "                short_put_delta = data[option, 'Delta Short']\n",
    "                long_put_delta = 0\n",
    "\n",
    "            trades['Put Delta', option].append(\n",
    "                abs(globals()['positions_put_' + option]) * (long_put_delta + short_put_delta))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr:last-of-type th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th colspan=\"3\" halign=\"left\">Call Delta</th>\n",
       "      <th colspan=\"3\" halign=\"left\">Call Position</th>\n",
       "      <th colspan=\"3\" halign=\"left\">Put Delta</th>\n",
       "      <th colspan=\"3\" halign=\"left\">Put Position</th>\n",
       "      <th>Time to Expiry</th>\n",
       "      <th>Total Option Delta</th>\n",
       "      <th>Stock Position</th>\n",
       "      <th>Remaining Option Delta</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th>C60</th>\n",
       "      <th>C70</th>\n",
       "      <th>C80</th>\n",
       "      <th>C60</th>\n",
       "      <th>C70</th>\n",
       "      <th>C80</th>\n",
       "      <th>P60</th>\n",
       "      <th>P70</th>\n",
       "      <th>P80</th>\n",
       "      <th>P60</th>\n",
       "      <th>P70</th>\n",
       "      <th>P80</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>Stock</th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Timestamp</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2018-02-28 23:35:00</th>\n",
       "      <td>281.812848</td>\n",
       "      <td>375.708432</td>\n",
       "      <td>117.148014</td>\n",
       "      <td>305.0</td>\n",
       "      <td>532.0</td>\n",
       "      <td>286.0</td>\n",
       "      <td>-7.017632</td>\n",
       "      <td>-10.388177</td>\n",
       "      <td>247.964456</td>\n",
       "      <td>95.0</td>\n",
       "      <td>36.0</td>\n",
       "      <td>-420.0</td>\n",
       "      <td>0.750038</td>\n",
       "      <td>1005.227940</td>\n",
       "      <td>-1005.0</td>\n",
       "      <td>0.227940</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-02-28 23:40:00</th>\n",
       "      <td>281.646451</td>\n",
       "      <td>375.009738</td>\n",
       "      <td>116.724180</td>\n",
       "      <td>305.0</td>\n",
       "      <td>532.0</td>\n",
       "      <td>286.0</td>\n",
       "      <td>-7.017525</td>\n",
       "      <td>-10.388147</td>\n",
       "      <td>248.586868</td>\n",
       "      <td>95.0</td>\n",
       "      <td>36.0</td>\n",
       "      <td>-420.0</td>\n",
       "      <td>0.750029</td>\n",
       "      <td>1004.561566</td>\n",
       "      <td>-1004.0</td>\n",
       "      <td>0.561566</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-02-28 23:45:00</th>\n",
       "      <td>281.646800</td>\n",
       "      <td>375.010163</td>\n",
       "      <td>116.723895</td>\n",
       "      <td>305.0</td>\n",
       "      <td>532.0</td>\n",
       "      <td>286.0</td>\n",
       "      <td>-7.017417</td>\n",
       "      <td>-10.388117</td>\n",
       "      <td>248.587287</td>\n",
       "      <td>95.0</td>\n",
       "      <td>36.0</td>\n",
       "      <td>-420.0</td>\n",
       "      <td>0.750019</td>\n",
       "      <td>1004.562612</td>\n",
       "      <td>-1004.0</td>\n",
       "      <td>0.562612</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-02-28 23:50:00</th>\n",
       "      <td>281.647150</td>\n",
       "      <td>375.010589</td>\n",
       "      <td>116.723609</td>\n",
       "      <td>305.0</td>\n",
       "      <td>532.0</td>\n",
       "      <td>286.0</td>\n",
       "      <td>-7.017310</td>\n",
       "      <td>-10.388087</td>\n",
       "      <td>248.587707</td>\n",
       "      <td>95.0</td>\n",
       "      <td>36.0</td>\n",
       "      <td>-420.0</td>\n",
       "      <td>0.750010</td>\n",
       "      <td>1004.563657</td>\n",
       "      <td>-1004.0</td>\n",
       "      <td>0.563657</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-02-28 23:55:00</th>\n",
       "      <td>281.479734</td>\n",
       "      <td>374.309980</td>\n",
       "      <td>116.299862</td>\n",
       "      <td>305.0</td>\n",
       "      <td>532.0</td>\n",
       "      <td>286.0</td>\n",
       "      <td>-7.118870</td>\n",
       "      <td>-10.481772</td>\n",
       "      <td>249.209993</td>\n",
       "      <td>95.0</td>\n",
       "      <td>36.0</td>\n",
       "      <td>-420.0</td>\n",
       "      <td>0.750000</td>\n",
       "      <td>1003.698927</td>\n",
       "      <td>-1003.0</td>\n",
       "      <td>0.698927</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                     Call Delta                         Call Position         \\\n",
       "                            C60         C70         C80           C60    C70   \n",
       "Timestamp                                                                      \n",
       "2018-02-28 23:35:00  281.812848  375.708432  117.148014         305.0  532.0   \n",
       "2018-02-28 23:40:00  281.646451  375.009738  116.724180         305.0  532.0   \n",
       "2018-02-28 23:45:00  281.646800  375.010163  116.723895         305.0  532.0   \n",
       "2018-02-28 23:50:00  281.647150  375.010589  116.723609         305.0  532.0   \n",
       "2018-02-28 23:55:00  281.479734  374.309980  116.299862         305.0  532.0   \n",
       "\n",
       "                           Put Delta                        Put Position  \\\n",
       "                       C80       P60        P70         P80          P60   \n",
       "Timestamp                                                                  \n",
       "2018-02-28 23:35:00  286.0 -7.017632 -10.388177  247.964456         95.0   \n",
       "2018-02-28 23:40:00  286.0 -7.017525 -10.388147  248.586868         95.0   \n",
       "2018-02-28 23:45:00  286.0 -7.017417 -10.388117  248.587287         95.0   \n",
       "2018-02-28 23:50:00  286.0 -7.017310 -10.388087  248.587707         95.0   \n",
       "2018-02-28 23:55:00  286.0 -7.118870 -10.481772  249.209993         95.0   \n",
       "\n",
       "                                 Time to Expiry Total Option Delta  \\\n",
       "                      P70    P80                                     \n",
       "Timestamp                                                            \n",
       "2018-02-28 23:35:00  36.0 -420.0       0.750038        1005.227940   \n",
       "2018-02-28 23:40:00  36.0 -420.0       0.750029        1004.561566   \n",
       "2018-02-28 23:45:00  36.0 -420.0       0.750019        1004.562612   \n",
       "2018-02-28 23:50:00  36.0 -420.0       0.750010        1004.563657   \n",
       "2018-02-28 23:55:00  36.0 -420.0       0.750000        1003.698927   \n",
       "\n",
       "                    Stock Position Remaining Option Delta  \n",
       "                             Stock                         \n",
       "Timestamp                                                  \n",
       "2018-02-28 23:35:00        -1005.0               0.227940  \n",
       "2018-02-28 23:40:00        -1004.0               0.561566  \n",
       "2018-02-28 23:45:00        -1004.0               0.562612  \n",
       "2018-02-28 23:50:00        -1004.0               0.563657  \n",
       "2018-02-28 23:55:00        -1003.0               0.698927  "
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Create DataFrame with Index Timestamp\n",
    "trades = pd.DataFrame(trades).set_index('Timestamp')\n",
    "\n",
    "# Sort Columns\n",
    "trades = trades.reindex(sorted(trades.columns), axis=1)\n",
    "\n",
    "# Calculate Total Option Delta (based on sorted columns)\n",
    "trades['Total Option Delta', ''] = np.sum(\n",
    "    trades['Call Delta'], axis=1) + np.sum(trades['Put Delta'], axis=1)\n",
    "\n",
    "# Calculate Cumulative Stock Position (floored if positive, ceiled if negative)\n",
    "trades['Stock Position', 'Stock'] = -np.where(trades['Total Option Delta', ''] >= 0, np.floor(\n",
    "    trades['Total Option Delta', '']), np.ceil(trades['Total Option Delta', '']))\n",
    "\n",
    "# Calculate remaining option delta (that remains unhedged)\n",
    "# This delta is included in the Total Option Delta again which ensures\n",
    "# It always remains below zero\n",
    "trades['Remaining Option Delta', ''] = trades['Total Option Delta',\n",
    "                                              ''] + trades['Stock Position', 'Stock']\n",
    "\n",
    "# Show DataFrame\n",
    "trades.tail()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Actual Trades/Volumes\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>C60</th>\n",
       "      <th>C70</th>\n",
       "      <th>C80</th>\n",
       "      <th>P60</th>\n",
       "      <th>P70</th>\n",
       "      <th>P80</th>\n",
       "      <th>Stock</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Timestamp</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2018-01-01 00:10:00</th>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-01-01 00:15:00</th>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-01-01 00:20:00</th>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-01-01 00:25:00</th>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-01-01 00:30:00</th>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                     C60  C70  C80  P60  P70  P80  Stock\n",
       "Timestamp                                               \n",
       "2018-01-01 00:10:00  0.0  0.0  0.0  0.0  0.0  0.0    0.0\n",
       "2018-01-01 00:15:00  0.0  0.0  0.0  0.0  0.0  0.0    0.0\n",
       "2018-01-01 00:20:00  0.0  0.0  0.0  0.0  0.0  0.0    0.0\n",
       "2018-01-01 00:25:00  0.0  0.0  0.0  0.0  0.0  0.0    0.0\n",
       "2018-01-01 00:30:00  0.0  0.0  0.0  0.0  0.0  0.0    0.0"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Final Positions that we currently 'own'\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>C60</th>\n",
       "      <th>C70</th>\n",
       "      <th>C80</th>\n",
       "      <th>P60</th>\n",
       "      <th>P70</th>\n",
       "      <th>P80</th>\n",
       "      <th>Stock</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Timestamp</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2018-02-28 23:55:00</th>\n",
       "      <td>305.0</td>\n",
       "      <td>532.0</td>\n",
       "      <td>286.0</td>\n",
       "      <td>95.0</td>\n",
       "      <td>36.0</td>\n",
       "      <td>-420.0</td>\n",
       "      <td>-1003.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                       C60    C70    C80   P60   P70    P80   Stock\n",
       "Timestamp                                                          \n",
       "2018-02-28 23:55:00  305.0  532.0  286.0  95.0  36.0 -420.0 -1003.0"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Create trades_diff dataframe that gives all actual trades (not positions)\n",
    "# Also drop columns that are not required for PnL calculations and such\n",
    "trades_diff = trades.diff()[1:].drop(\n",
    "    ['Call Delta', 'Put Delta', 'Time to Expiry', 'Total Option Delta', 'Remaining Option Delta'], axis=1)\n",
    "\n",
    "# Drop the 'Call Position','Put Position' and 'Stock Position' top level\n",
    "# Makes forlooping easier\n",
    "trades_diff.columns = trades_diff.columns.droplevel(level=0)\n",
    "\n",
    "# Since positions are not neccesarily zero at the last timestamp, final positions are calculated to be able to valuate these\n",
    "final_positions = trades[-1:].drop(['Call Delta', 'Put Delta', 'Time to Expiry',\n",
    "                                    'Total Option Delta', 'Remaining Option Delta'], axis=1)\n",
    "\n",
    "final_positions.columns = final_positions.columns.droplevel(level=0)\n",
    "\n",
    "# Show DataFrames\n",
    "print('Actual Trades/Volumes')\n",
    "display(trades_diff.head())\n",
    "\n",
    "print(\"Final Positions that we currently 'own'\")\n",
    "display(final_positions)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Including Timestamp again to match trades_diff index\n",
    "market_data['Timestamp'] = timestamp\n",
    "market_data = market_data.set_index('Timestamp')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Stock</th>\n",
       "      <th>P60</th>\n",
       "      <th>P70</th>\n",
       "      <th>P80</th>\n",
       "      <th>C60</th>\n",
       "      <th>C70</th>\n",
       "      <th>C80</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Timestamp</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2018-01-01 00:10:00</th>\n",
       "      <td>-0.0</td>\n",
       "      <td>-0.0</td>\n",
       "      <td>-0.0</td>\n",
       "      <td>-0.0</td>\n",
       "      <td>-0.0</td>\n",
       "      <td>-0.0</td>\n",
       "      <td>-0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-01-01 00:15:00</th>\n",
       "      <td>-0.0</td>\n",
       "      <td>-0.0</td>\n",
       "      <td>-0.0</td>\n",
       "      <td>-0.0</td>\n",
       "      <td>-0.0</td>\n",
       "      <td>-0.0</td>\n",
       "      <td>-0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-01-01 00:20:00</th>\n",
       "      <td>-0.0</td>\n",
       "      <td>-0.0</td>\n",
       "      <td>-0.0</td>\n",
       "      <td>-0.0</td>\n",
       "      <td>-0.0</td>\n",
       "      <td>-0.0</td>\n",
       "      <td>-0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-01-01 00:25:00</th>\n",
       "      <td>-0.0</td>\n",
       "      <td>-0.0</td>\n",
       "      <td>-0.0</td>\n",
       "      <td>-0.0</td>\n",
       "      <td>-0.0</td>\n",
       "      <td>-0.0</td>\n",
       "      <td>-0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-01-01 00:30:00</th>\n",
       "      <td>-0.0</td>\n",
       "      <td>-0.0</td>\n",
       "      <td>-0.0</td>\n",
       "      <td>-0.0</td>\n",
       "      <td>-0.0</td>\n",
       "      <td>-0.0</td>\n",
       "      <td>-0.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                     Stock  P60  P70  P80  C60  C70  C80\n",
       "Timestamp                                               \n",
       "2018-01-01 00:10:00   -0.0 -0.0 -0.0 -0.0 -0.0 -0.0 -0.0\n",
       "2018-01-01 00:15:00   -0.0 -0.0 -0.0 -0.0 -0.0 -0.0 -0.0\n",
       "2018-01-01 00:20:00   -0.0 -0.0 -0.0 -0.0 -0.0 -0.0 -0.0\n",
       "2018-01-01 00:25:00   -0.0 -0.0 -0.0 -0.0 -0.0 -0.0 -0.0\n",
       "2018-01-01 00:30:00   -0.0 -0.0 -0.0 -0.0 -0.0 -0.0 -0.0"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The total Cashflow is: € 68985.54\n"
     ]
    }
   ],
   "source": [
    "# Create Dataframe with index market_data (timestamp)\n",
    "cashflow_dataframe = pd.DataFrame(index=market_data.index[1:])\n",
    "\n",
    "# Forloop on all instruments (including stock) to calculate PnL\n",
    "for instrument in instrument_names:\n",
    "\n",
    "    Instrument_AskPrice = market_data[instrument, 'AskPrice'][1:]\n",
    "    Instrument_BidPrice = market_data[instrument, 'BidPrice'][1:]\n",
    "\n",
    "    cashflow_dataframe[instrument] = np.where(trades_diff[instrument] >= 0,\n",
    "                                              trades_diff[instrument] * -\n",
    "                                              Instrument_AskPrice,\n",
    "                                              trades_diff[instrument] * -Instrument_BidPrice)\n",
    "\n",
    "# Show DataFrame & PnL\n",
    "display(cashflow_dataframe.head())\n",
    "total_cashflow = cashflow_dataframe.sum().sum()\n",
    "\n",
    "print('The total Cashflow is: €', round(total_cashflow, 2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Stock</th>\n",
       "      <th>P60</th>\n",
       "      <th>P70</th>\n",
       "      <th>P80</th>\n",
       "      <th>C60</th>\n",
       "      <th>C70</th>\n",
       "      <th>C80</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Timestamp</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2018-02-28 23:35:00</th>\n",
       "      <td>71187.00</td>\n",
       "      <td>-116.36</td>\n",
       "      <td>762.03</td>\n",
       "      <td>4326.29</td>\n",
       "      <td>-3141.69</td>\n",
       "      <td>-3354.85</td>\n",
       "      <td>-525.08</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-02-28 23:40:00</th>\n",
       "      <td>71111.05</td>\n",
       "      <td>-116.36</td>\n",
       "      <td>762.03</td>\n",
       "      <td>4326.29</td>\n",
       "      <td>-3141.69</td>\n",
       "      <td>-3354.85</td>\n",
       "      <td>-525.08</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-02-28 23:45:00</th>\n",
       "      <td>71111.05</td>\n",
       "      <td>-116.36</td>\n",
       "      <td>762.03</td>\n",
       "      <td>4326.29</td>\n",
       "      <td>-3141.69</td>\n",
       "      <td>-3354.85</td>\n",
       "      <td>-525.08</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-02-28 23:50:00</th>\n",
       "      <td>71111.05</td>\n",
       "      <td>-116.36</td>\n",
       "      <td>762.03</td>\n",
       "      <td>4326.29</td>\n",
       "      <td>-3141.69</td>\n",
       "      <td>-3354.85</td>\n",
       "      <td>-525.08</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-02-28 23:55:00</th>\n",
       "      <td>71035.20</td>\n",
       "      <td>-116.36</td>\n",
       "      <td>762.03</td>\n",
       "      <td>4326.29</td>\n",
       "      <td>-3141.69</td>\n",
       "      <td>-3354.85</td>\n",
       "      <td>-525.08</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                        Stock     P60     P70      P80      C60      C70  \\\n",
       "Timestamp                                                                  \n",
       "2018-02-28 23:35:00  71187.00 -116.36  762.03  4326.29 -3141.69 -3354.85   \n",
       "2018-02-28 23:40:00  71111.05 -116.36  762.03  4326.29 -3141.69 -3354.85   \n",
       "2018-02-28 23:45:00  71111.05 -116.36  762.03  4326.29 -3141.69 -3354.85   \n",
       "2018-02-28 23:50:00  71111.05 -116.36  762.03  4326.29 -3141.69 -3354.85   \n",
       "2018-02-28 23:55:00  71035.20 -116.36  762.03  4326.29 -3141.69 -3354.85   \n",
       "\n",
       "                        C80  \n",
       "Timestamp                    \n",
       "2018-02-28 23:35:00 -525.08  \n",
       "2018-02-28 23:40:00 -525.08  \n",
       "2018-02-28 23:45:00 -525.08  \n",
       "2018-02-28 23:50:00 -525.08  \n",
       "2018-02-28 23:55:00 -525.08  "
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "This number should match the above number: € 68985.54\n"
     ]
    }
   ],
   "source": [
    "# Cumulative Cashflows\n",
    "cashflow_cumulative = {}\n",
    "\n",
    "for column in cashflow_dataframe.columns:\n",
    "\n",
    "    cashflow_cumulative[column] = cashflow_dataframe[column].cumsum()\n",
    "\n",
    "cashflow_cumulative = pd.DataFrame(cashflow_cumulative)\n",
    "\n",
    "# Show Cumulative Cashflow\n",
    "display(cashflow_cumulative.tail())\n",
    "\n",
    "# Checking for Match\n",
    "print('This number should match the above number: €',\n",
    "      round(cashflow_cumulative[-1:].sum().sum(), 2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Stock</th>\n",
       "      <th>P60</th>\n",
       "      <th>P70</th>\n",
       "      <th>P80</th>\n",
       "      <th>C60</th>\n",
       "      <th>C70</th>\n",
       "      <th>C80</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Timestamp</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2018-02-28 23:35:00</th>\n",
       "      <td>-76329.75</td>\n",
       "      <td>46.55</td>\n",
       "      <td>93.24</td>\n",
       "      <td>-3238.2</td>\n",
       "      <td>4971.50</td>\n",
       "      <td>4500.72</td>\n",
       "      <td>986.70</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-02-28 23:40:00</th>\n",
       "      <td>-76253.80</td>\n",
       "      <td>45.60</td>\n",
       "      <td>91.80</td>\n",
       "      <td>-3234.0</td>\n",
       "      <td>4974.55</td>\n",
       "      <td>4516.68</td>\n",
       "      <td>975.26</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-02-28 23:45:00</th>\n",
       "      <td>-76253.80</td>\n",
       "      <td>46.55</td>\n",
       "      <td>90.36</td>\n",
       "      <td>-3246.6</td>\n",
       "      <td>4962.35</td>\n",
       "      <td>4495.40</td>\n",
       "      <td>978.12</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-02-28 23:50:00</th>\n",
       "      <td>-76253.80</td>\n",
       "      <td>43.70</td>\n",
       "      <td>90.00</td>\n",
       "      <td>-3234.0</td>\n",
       "      <td>4974.55</td>\n",
       "      <td>4532.64</td>\n",
       "      <td>989.56</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-02-28 23:55:00</th>\n",
       "      <td>-76077.55</td>\n",
       "      <td>44.65</td>\n",
       "      <td>90.36</td>\n",
       "      <td>-3280.2</td>\n",
       "      <td>4944.05</td>\n",
       "      <td>4500.72</td>\n",
       "      <td>975.26</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                        Stock    P60    P70     P80      C60      C70     C80\n",
       "Timestamp                                                                    \n",
       "2018-02-28 23:35:00 -76329.75  46.55  93.24 -3238.2  4971.50  4500.72  986.70\n",
       "2018-02-28 23:40:00 -76253.80  45.60  91.80 -3234.0  4974.55  4516.68  975.26\n",
       "2018-02-28 23:45:00 -76253.80  46.55  90.36 -3246.6  4962.35  4495.40  978.12\n",
       "2018-02-28 23:50:00 -76253.80  43.70  90.00 -3234.0  4974.55  4532.64  989.56\n",
       "2018-02-28 23:55:00 -76077.55  44.65  90.36 -3280.2  4944.05  4500.72  975.26"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Total valuation of our Position is currently: € -68802.71\n"
     ]
    }
   ],
   "source": [
    "# Create a new dataframe of trades with most columns dropped\n",
    "trades_minimal = trades.drop(['Call Delta', 'Put Delta', 'Time to Expiry', 'Total Option Delta',\n",
    "                              'Remaining Option Delta'], axis=1)\n",
    "\n",
    "trades_minimal.columns = trades_minimal.columns.droplevel(level=0)\n",
    "\n",
    "# Create Dataframe with market_data as index\n",
    "valuation_dataframe = pd.DataFrame(index=market_data.index)\n",
    "\n",
    "# Forloop to calculate valuations on every timestamp\n",
    "for instrument in instrument_names:\n",
    "\n",
    "    if 'C' in instrument:\n",
    "\n",
    "        Instrument_AskPrice = market_data[instrument, 'AskPrice']\n",
    "        Instrument_BidPrice = market_data[instrument, 'BidPrice']\n",
    "\n",
    "        valuation_dataframe[instrument] = np.where(trades_minimal[instrument] > 0,\n",
    "                                                   trades_minimal[instrument] *\n",
    "                                                   Instrument_BidPrice,\n",
    "                                                   trades_minimal[instrument] * Instrument_AskPrice)\n",
    "\n",
    "    if 'P' in instrument:\n",
    "\n",
    "        Instrument_AskPrice = market_data[instrument, 'AskPrice']\n",
    "        Instrument_BidPrice = market_data[instrument, 'BidPrice']\n",
    "\n",
    "        valuation_dataframe[instrument] = np.where(trades_minimal[instrument] > 0,\n",
    "                                                   trades_minimal[instrument] *\n",
    "                                                   Instrument_BidPrice,\n",
    "                                                   trades_minimal[instrument] * Instrument_AskPrice)\n",
    "\n",
    "    if 'S' in instrument:\n",
    "\n",
    "        Instrument_AskPrice = market_data[instrument, 'AskPrice']\n",
    "        Instrument_BidPrice = market_data[instrument, 'BidPrice']\n",
    "\n",
    "        valuation_dataframe[instrument] = np.where(trades_minimal[instrument] > 0,\n",
    "                                                   trades_minimal[instrument] *\n",
    "                                                   Instrument_BidPrice,\n",
    "                                                   trades_minimal[instrument] * Instrument_AskPrice)\n",
    "\n",
    "# Show DataFrame & Calculate total valuation\n",
    "display(valuation_dataframe.tail())\n",
    "total_valuation = valuation_dataframe[-1:].sum().sum()\n",
    "\n",
    "print(\"Total valuation of our Position is currently: €\", round(total_valuation, 2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The total profit from Black Scholes is: € 547.08\n"
     ]
    }
   ],
   "source": [
    "# Create Empty DataFrame\n",
    "blackscholes_dataframe = {}\n",
    "\n",
    "# Create Columns based on Option Names\n",
    "for option in option_names:\n",
    "\n",
    "    if 'C' in option:\n",
    "        blackscholes_dataframe[option] = []\n",
    "\n",
    "    if 'P' in option:\n",
    "        blackscholes_dataframe[option] = []\n",
    "\n",
    "# Forloop that calculates the margins and thus profits\n",
    "for time, data in market_data.iterrows():\n",
    "    \n",
    "    for option in option_names:\n",
    "\n",
    "        if \"C\" in option:\n",
    "            margin1 = data[option, 'BidPrice'] - data[option, 'Expected AskPrice']\n",
    "            margin2 = data[option, 'Expected BidPrice'] - data[option, 'AskPrice']\n",
    "\n",
    "            if margin1 > 0.10:\n",
    "                blackscholes_dataframe[option].append(margin1)\n",
    "\n",
    "            elif margin2 > 0.10:\n",
    "                blackscholes_dataframe[option].append(margin2)\n",
    "\n",
    "            else:\n",
    "                blackscholes_dataframe[option].append(0)\n",
    "\n",
    "        elif \"P\" in option:\n",
    "            margin1 = data[option, 'BidPrice'] - data[option, 'Expected AskPrice']\n",
    "            margin2 = data[option, 'Expected BidPrice'] - data[option, 'AskPrice']\n",
    "\n",
    "            if margin1 > 0.10:\n",
    "                blackscholes_dataframe[option].append(margin1)\n",
    "\n",
    "            elif margin2 > 0.10:\n",
    "                blackscholes_dataframe[option].append(margin2)\n",
    "\n",
    "            else:\n",
    "                blackscholes_dataframe[option].append(0)\n",
    "\n",
    "# Create DataFrame with index of market_data\n",
    "blackscholes_dataframe = pd.DataFrame(blackscholes_dataframe, index=market_data.index)\n",
    "\n",
    "# Calculate Black_Scholes Profit\n",
    "total_blackscholes = (abs(trades_diff).drop('Stock', axis=1) * blackscholes_dataframe).sum().sum()\n",
    "print('The total profit from Black Scholes is: €',round(total_blackscholes,2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>C60</th>\n",
       "      <th>C70</th>\n",
       "      <th>C80</th>\n",
       "      <th>P60</th>\n",
       "      <th>P70</th>\n",
       "      <th>P80</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Timestamp</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2018-02-28 23:35:00</th>\n",
       "      <td>61.95</td>\n",
       "      <td>77.29</td>\n",
       "      <td>88.98</td>\n",
       "      <td>14.33</td>\n",
       "      <td>135.36</td>\n",
       "      <td>169.17</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-02-28 23:40:00</th>\n",
       "      <td>61.95</td>\n",
       "      <td>77.29</td>\n",
       "      <td>88.98</td>\n",
       "      <td>14.33</td>\n",
       "      <td>135.36</td>\n",
       "      <td>169.17</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-02-28 23:45:00</th>\n",
       "      <td>61.95</td>\n",
       "      <td>77.29</td>\n",
       "      <td>88.98</td>\n",
       "      <td>14.33</td>\n",
       "      <td>135.36</td>\n",
       "      <td>169.17</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-02-28 23:50:00</th>\n",
       "      <td>61.95</td>\n",
       "      <td>77.29</td>\n",
       "      <td>88.98</td>\n",
       "      <td>14.33</td>\n",
       "      <td>135.36</td>\n",
       "      <td>169.17</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-02-28 23:55:00</th>\n",
       "      <td>61.95</td>\n",
       "      <td>77.29</td>\n",
       "      <td>88.98</td>\n",
       "      <td>14.33</td>\n",
       "      <td>135.36</td>\n",
       "      <td>169.17</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                       C60    C70    C80    P60     P70     P80\n",
       "Timestamp                                                      \n",
       "2018-02-28 23:35:00  61.95  77.29  88.98  14.33  135.36  169.17\n",
       "2018-02-28 23:40:00  61.95  77.29  88.98  14.33  135.36  169.17\n",
       "2018-02-28 23:45:00  61.95  77.29  88.98  14.33  135.36  169.17\n",
       "2018-02-28 23:50:00  61.95  77.29  88.98  14.33  135.36  169.17\n",
       "2018-02-28 23:55:00  61.95  77.29  88.98  14.33  135.36  169.17"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Blackscholes Dataframe times Volumes\n",
    "blackscholes_dataframe = pd.DataFrame(\n",
    "    abs(trades_diff).drop('Stock', axis=1) * blackscholes_dataframe)\n",
    "\n",
    "# Cumulative Blackscholes\n",
    "blackscholes_cumulative = {}\n",
    "\n",
    "for column in blackscholes_dataframe.columns:\n",
    "\n",
    "    blackscholes_cumulative[column] = blackscholes_dataframe[column].cumsum()\n",
    "\n",
    "blackscholes_cumulative = pd.DataFrame(blackscholes_cumulative)\n",
    "\n",
    "# Show Dataframe\n",
    "blackscholes_cumulative.tail()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The total profit generated from the Option Arbitrage strategy is: € 729.91\n"
     ]
    }
   ],
   "source": [
    "# Last PnL in the Table should match the below value\n",
    "print('The total profit generated from the Option Arbitrage strategy is: €',\n",
    "      round(total_cashflow + total_valuation + total_blackscholes, 2))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.1"
  },
  "latex_envs": {
   "LaTeX_envs_menu_present": true,
   "autoclose": false,
   "autocomplete": true,
   "bibliofile": "biblio.bib",
   "cite_by": "apalike",
   "current_citInitial": 1,
   "eqLabelWithNumbers": true,
   "eqNumInitial": 1,
   "hotkeys": {
    "equation": "Ctrl-E",
    "itemize": "Ctrl-I"
   },
   "labels_anchors": false,
   "latex_user_defs": false,
   "report_style_numbering": false,
   "user_envs_cfg": false
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
